package cn.wolfcode.wolf2w.web.controller;

import cn.wolfcode.wolf2w.annotation.RequestLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.cache.service.IStrategyRedisService;
import cn.wolfcode.wolf2w.cache.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;

@RestController
@RequestMapping("strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCommentService strategyCommentService;

    @Autowired
    private IStrategyRedisService strategyRedisService;

    /**
     * 攻略阅读数+1
     * @param id
     * @return
     */
    @GetMapping("detail")
    public Object detail(String id){
        Strategy strategy = strategyService.get(id);

        // 攻略阅读数+1
        strategyRedisService.setReadNum(id,1);

        return JsonResult.success(strategy);
    }

    /**
     * 通过目的地id查询对应的所有攻略明细
     * @param destId
     * @return
     */
    @GetMapping("list")
    public Object list(String destId){
        List<Strategy> strategyList = strategyService.queryStrategyByDestId(destId);
        return JsonResult.success(strategyList);
    }

    /**
     * 查询攻略的主题
     * @return
     */
    @GetMapping("themes")
    public Object themes(){
        List<StrategyTheme> list = strategyThemeService.list();
        return JsonResult.success(list);
    }

    /**
     * 攻略的分页
     * @param qo
     * @return
     */
    @GetMapping("query")
    public Object query(StrategyQuery qo){
        Page<Strategy> page = strategyService.query(qo);
        return JsonResult.success(page);
    }

    /**
     * 在攻略添加评论
     * @param strategyComment
     * @param userInfo
     * @return
     */
    @RequestLogin // 需要用户登录
    @PostMapping("addComment")
    public Object addComment(StrategyComment strategyComment, @UserParam UserInfo userInfo){

        // 参数1:源对象  参数2:目标对象
        // 把源对象与目标对象相同的属性拷贝到目标对象（实现原理：内省机制）
        BeanUtils.copyProperties(userInfo,strategyComment);

        // id属性是唯一的,所有不能进行拷贝
        strategyComment.setUserId(userInfo.getId());
        // 将评论数据保存到数据库中
        strategyCommentService.save(strategyComment);

        // 在添加评论之后进行评论数 +1操作 (攻略id)
        strategyRedisService.replyNumIncrease(strategyComment.getStrategyId(),1);

        return JsonResult.success();
    }

    /**
     * 攻略评论信息的显示(分页)
     * @param qo
     * @return
     */
    @GetMapping("comments")
    public Object comments(StrategyCommentQuery qo){
        Page<StrategyComment> page = strategyCommentService.query(qo);
        return JsonResult.success(page);
    }

    /**
     * 评论中点赞与取消点赞
     * @param cid
     * @return
     */
    @RequestLogin // 需要用户登录
    @PostMapping("commentThumb")
    public Object commentThumb(String cid,@UserParam UserInfo userInfo){
        // 评论中点赞与取消点赞操作
        strategyCommentService.commentThumb(cid,userInfo);
        return JsonResult.success();
    }

    /**
     * 获取vo对象 （用于统计数据）
     * @param sid
     * @return
     */
    @GetMapping("statisVo")
    public Object statisVo(String sid){
        // 用于vo对象里面评论数/访问数/收藏数/分享数/点赞个数 数据查询
        // 封装成一个对象
        StrategyStatisVO strategyStatisVo = strategyRedisService.getStrategyStatisVo(sid);
        return JsonResult.success(strategyStatisVo);
    }

    /**
     * 攻略的收藏与取消收藏
     * @param userInfo
     * @param sid
     * @return
     */
    @RequestLogin
    @PostMapping("favor")
    public Object favor(@UserParam UserInfo userInfo,String sid){
        // 攻略明细中的收藏与取消收藏操作
        boolean result = strategyRedisService.setfavorNum(userInfo.getId(),sid);
        return JsonResult.success(result);
    }

    /**
     * 攻略的顶操作
     * @param sid
     * @param userInfo
     * @return
     */
    @RequestLogin
    @PostMapping("strategyThumbup")
    public Object strategyThumbup(String sid,@UserParam UserInfo userInfo){
        // 攻略中的顶操作（每天只能顶一次） 返回boolean类型进行判断
        boolean thumbuop = strategyRedisService.setThumbuop(sid, userInfo.getId());
        return JsonResult.success(thumbuop);
    }

}
